package com.itheima.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.project.constant.SuperConstant;
import com.itheima.project.mapper.DataDictMapper;
import com.itheima.project.pojo.DataDict;
import com.itheima.project.service.IDataDictService;
import com.itheima.project.utils.EmptyUtil;
import com.itheima.project.vo.dict.DataDictVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description：数据字典表 服务实现类
 */
@Service
public class DataDictServiceImpl extends ServiceImpl<DataDictMapper, DataDict> implements IDataDictService {

    @Override
    public Page<DataDict> findDataDictVoPage(DataDictVo dataDictVo, int pageNum, int pageSize) {
        Page<DataDict> page = new Page<>(pageNum, pageSize);
        QueryWrapper<DataDict> queryWrapper = new QueryWrapper<>();
        //多条件查询
        if (!EmptyUtil.isNullOrEmpty(dataDictVo.getDiscriptioin())){
            queryWrapper.lambda().like(DataDict::getDiscriptioin,dataDictVo.getDiscriptioin());
        }
        if (!EmptyUtil.isNullOrEmpty(dataDictVo.getParentKey())){
            queryWrapper.lambda().eq(DataDict::getParentKey,dataDictVo.getParentKey());
        }
        if (!EmptyUtil.isNullOrEmpty(dataDictVo.getDataKey())){
            queryWrapper.lambda().eq(DataDict::getDataKey,dataDictVo.getDataKey());
        }
        if (!EmptyUtil.isNullOrEmpty(dataDictVo.getDataValue())){
            queryWrapper.lambda().eq(DataDict::getDataValue,dataDictVo.getDataValue());
        }
        queryWrapper.lambda().orderByAsc(DataDict::getParentKey);
        //执行查询
        return page(page, queryWrapper);
    }

    @Override
    public Boolean checkByDataKey(String dataKey) {
        QueryWrapper<DataDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DataDict::getDataKey,dataKey);
        DataDict dataDict = getOne(queryWrapper);
        return !EmptyUtil.isNullOrEmpty(dataKey);
    }

    @Override
    public Set<String> findParentKeyAll() {
        QueryWrapper<DataDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DataDict::getEnableFlag, SuperConstant.YES);
        List<DataDict> list = list(queryWrapper);
        if (!EmptyUtil.isNullOrEmpty(list)){
            return list.stream().map(DataDict::getParentKey).collect(Collectors.toSet());
        }
        return null;
    }

    @Override
    public List<DataDict> findDataDictByParentKey(String parentKey) {
        QueryWrapper<DataDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DataDict::getParentKey,parentKey)
                .eq(DataDict::getEnableFlag, SuperConstant.YES);
        return list(queryWrapper);
    }

    @Override
    public Set<String> findDataKeyAll() {
        QueryWrapper<DataDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DataDict::getEnableFlag, SuperConstant.YES);
        List<DataDict> list = list(queryWrapper);
        if (!EmptyUtil.isNullOrEmpty(list)){
            return list.stream().map(DataDict::getDataKey).collect(Collectors.toSet());
        }
        return null;
    }

    @Override
    public DataDict findDataDictVoByDataKey(String dataKey) {
        QueryWrapper<DataDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DataDict::getDataKey,dataKey)
                .eq(DataDict::getEnableFlag, SuperConstant.YES);
        return getOne(queryWrapper);
    }

}
